home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / updates / update36.zoo / libg++ / iosrc / strops.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-27  |  7.4 KB  |  291 lines

  1. /* 
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24.  
  25. #include "strfile.h"
  26. #include "libioP.h"
  27.  
  28. #define LEN(fp) (((_IO_strfile*)(fp))->_s._len)
  29.  
  30. #ifdef TODO
  31. /* An "unbounded buffer" is when a buffer is supplied, but with no
  32.    specified length.  An example is the buffer argument to sprintf.
  33.    */
  34. #endif
  35.  
  36. void
  37. _IO_str_init_static (fp, ptr, size, pstart)
  38.      _IO_FILE *fp;
  39.      char *ptr;
  40.      int size;
  41.      char *pstart;
  42. {
  43.   if (size == 0)
  44.     size = strlen(ptr);
  45.   else if (size < 0)
  46.     {
  47.       /* If size is negative 'the characters are assumed to
  48.      continue indefinitely.'  This is kind of messy ... */
  49. #if 1
  50.       int s;
  51.       size = 512;
  52.       /* Try increasing powers of 2, as long as we don't wrap around.
  53.      This can lose in pathological cases (ptr near the end
  54.      of the address space).  A better solution might be to
  55.      adjust the size on underflow/overflow.  FIXME. */
  56.       for (s; s = 2*size, s > 0 && ptr + s > ptr && s < 0x4000000L; )
  57.     size = s;
  58.       size = s;
  59. #else
  60.       /* The following semi-portable kludge assumes that
  61.      sizeof(unsigned long) == sizeof(char*). Hence,
  62.      (unsigned long)(-1) should be the largest possible address. */
  63.       unsigned long highest = (unsigned long)(-1);
  64.       /* Pointers are signed on some brain-damaged systems, in
  65.      which case we divide by two to get the maximum signed address. */
  66.       if  ((char*)highest < ptr)
  67.     highest >>= 1;
  68.       size = (char*)highest - ptr;
  69. #endif
  70.     }
  71.   _IO_setb(fp, ptr, ptr+size, 0);
  72.  
  73.   fp->_IO_write_base = ptr;
  74.   fp->_IO_read_base = ptr;
  75.   fp->_IO_read_ptr = ptr;
  76.   if (pstart)
  77.     {
  78.       fp->_IO_write_ptr = pstart;
  79.       fp->_IO_write_end = ptr+size;
  80.       fp->_IO_read_end = pstart;
  81.     }
  82.   else
  83.     {
  84.       fp->_IO_write_ptr = ptr;
  85.       fp->_IO_write_end = ptr;
  86.       fp->_IO_read_end = ptr+size;
  87.     }
  88.   LEN(fp) = size;
  89.   /* A null _allocate_buffer function flags the strfile as being static. */
  90.   (((_IO_strfile*)(fp))->_s._allocate_buffer) =  (_IO_alloc_type)0;
  91. }
  92.  
  93. void
  94. _IO_str_init_readonly (fp, ptr, size)
  95.      _IO_FILE *fp;
  96.      const char *ptr;
  97.      int size;
  98. {
  99.   _IO_str_init_static (fp, (char*)ptr, size, NULL);
  100.   fp->_IO_file_flags |= _IO_NO_WRITES;
  101. }
  102.  
  103. int _IO_str_overflow (fp, c)
  104.      register _IO_FILE* fp;
  105.      int c;
  106. {
  107.   int flush_only = c == EOF;
  108.   _IO_size_t pos = fp->_IO_write_ptr - fp->_IO_write_base;
  109.   _IO_size_t get_pos = fp->_IO_read_ptr - fp->_IO_read_base;
  110.   if (fp->_flags & _IO_NO_WRITES)
  111.       return flush_only ? 0 : EOF;
  112.   if (pos > LEN(fp)) LEN(fp) = pos;
  113.   if ((fp->_flags & _IO_TIED_PUT_GET) && !(fp->_flags & _IO_CURRENTLY_PUTTING))
  114.     {
  115.       pos = get_pos;
  116.       fp->_flags |= _IO_CURRENTLY_PUTTING;
  117.       get_pos = LEN(fp);
  118.     }
  119.   if (pos >= _IO_blen(fp) + flush_only)
  120.     {
  121.       if (fp->_flags & _IO_USER_BUF) /* not allowed to enlarge */
  122.     {
  123. #ifdef TODO
  124.       if (indefinite size)
  125.         {
  126.           fp->_IO_buf_end += 512;
  127.         }
  128.       else
  129. #endif
  130.       return EOF;
  131.     }
  132.       else
  133.     {
  134.       char *new_buf;
  135.       _IO_size_t new_size = 2 * _IO_blen(fp);
  136.       new_buf
  137.         = (char*)(*((_IO_strfile*)fp)->_s._allocate_buffer)(new_size);
  138.       if (new_buf == NULL)
  139.         {
  140.           /*      __ferror(fp) = 1; */
  141.           return EOF;
  142.         }
  143.       memcpy(new_buf, fp->_IO_buf_base, _IO_blen(fp));
  144. #if 0
  145.       if (lenp == &LEN(fp)) /* use '\0'-filling */
  146.           memset(new_buf + pos, 0, blen() - pos);
  147. #endif
  148.       if (fp->_IO_buf_base)
  149.         {
  150.           (*((_IO_strfile*)fp)->_s._free_buffer)(fp->_IO_buf_base);
  151.           /* Make sure _IO_setb won't try to delete _IO_buf_base. */
  152.           fp->_IO_buf_base = NULL;
  153.         }
  154.       _IO_setb(fp, new_buf, new_buf + new_size, 1);
  155.       fp->_IO_write_base = new_buf;
  156.     }
  157.       fp->_IO_write_end = fp->_IO_buf_end;
  158.     }
  159.  
  160.   fp->_IO_write_ptr = fp->_IO_buf_base + pos;
  161.  
  162.   fp->_IO_read_base = fp->_IO_buf_base;
  163.   fp->_IO_read_ptr = fp->_IO_buf_base + get_pos;;
  164.   fp->_IO_read_end = fp->_IO_buf_base + LEN(fp);;
  165.  
  166.   if (!flush_only)
  167.     *fp->_IO_write_ptr++ = (unsigned char) c;
  168.   return c;
  169. }
  170.  
  171. int
  172. _IO_str_underflow (fp)
  173.      register _IO_FILE* fp;
  174. {
  175.   _IO_size_t ppos = fp->_IO_write_ptr - fp->_IO_write_base;
  176.   if (ppos > LEN(fp)) LEN(fp) = ppos;
  177.   if ((fp->_flags & _IO_TIED_PUT_GET) && (fp->_flags & _IO_CURRENTLY_PUTTING))
  178.     {
  179.       fp->_flags &= ~_IO_CURRENTLY_PUTTING;
  180.       fp->_IO_write_ptr = fp->_IO_write_end;
  181.     }
  182.   fp->_IO_read_end = fp->_IO_read_base + LEN(fp);
  183.   if (fp->_IO_read_ptr < fp->_IO_read_end)
  184.     return *fp->_IO_read_ptr;
  185.   else
  186.     return EOF;
  187. }
  188.  
  189. _IO_ssize_t
  190. _IO_str_count (fp)
  191.      register _IO_FILE *fp;
  192. {
  193.   _IO_ssize_t put_len = fp->_IO_write_ptr - fp->_IO_write_base;
  194.   if (put_len < ((_IO_strfile*)fp)->_s._len)
  195.     put_len = ((_IO_strfile*)fp)->_s._len;
  196.   return put_len;
  197. }     
  198.  
  199. _IO_pos_t
  200. _IO_str_seekoff(fp, offset, mode)
  201.      register _IO_FILE *fp;
  202.      _IO_off_t offset;
  203.      _IO_seekflags mode;
  204. {
  205.   _IO_ssize_t cur_size = _IO_str_count(fp);
  206.   _IO_pos_t new_pos = EOF;
  207.   int dir = mode & 3;
  208.  
  209.   /* Move the get pointer, if requested. */
  210.   if (!(mode & _IO_seek_not_in))
  211.     {
  212.       switch (dir)
  213.     {
  214.     case _IO_seek_end:
  215.       offset += cur_size;
  216.       break;
  217.     case _IO_seek_cur:
  218.       offset += fp->_IO_read_ptr - fp->_IO_read_base;
  219.       break;
  220.     default: /* case _IO_seek_set: */
  221.       break;
  222.     }
  223.       if (offset < 0 || (_IO_size_t)offset > cur_size)
  224.     return EOF;
  225.       fp->_IO_read_ptr = fp->_IO_read_base + offset;
  226.       fp->_IO_read_end = fp->_IO_read_base + cur_size;
  227.       new_pos = offset;
  228.     }
  229.  
  230.   /* Move the put pointer, if requested. */
  231.   if (!(mode & _IO_seek_not_out))
  232.     {
  233.       switch (dir)
  234.     {
  235.     case _IO_seek_end:
  236.       offset += cur_size;
  237.       break;
  238.     case _IO_seek_cur:
  239.       offset += fp->_IO_write_ptr - fp->_IO_write_base;
  240.       break;
  241.     default: /* case _IO_seek_set: */
  242.       break;
  243.     }
  244.       if (offset < 0 || (_IO_size_t)offset > cur_size)
  245.     return EOF;
  246.       fp->_IO_write_ptr = fp->_IO_write_base + offset;
  247.       new_pos = offset;
  248.     }
  249.   return new_pos;
  250. }
  251.  
  252. int
  253. _IO_str_pbackfail(fp, c)
  254.      register _IO_FILE *fp;
  255.      int c;
  256. {
  257.   if ((fp->_flags & _IO_NO_WRITES) && c != EOF)
  258.     return EOF;
  259.   return _IO_default_pbackfail(fp, c);
  260. }
  261.  
  262. void
  263. _IO_str_finish(fp)
  264.      register _IO_FILE* fp;
  265. {
  266.   if (fp->_IO_buf_base && !(fp->_flags & _IO_USER_BUF))
  267.     (((_IO_strfile*)fp)->_s._free_buffer)(fp->_IO_buf_base);
  268.   fp->_IO_buf_base = NULL;
  269.  
  270.   _IO_default_finish(fp);
  271. }
  272.  
  273. struct _IO_jump_t _IO_str_jumps = {
  274.   _IO_str_overflow,
  275.   _IO_str_underflow,
  276.   _IO_default_xsputn,
  277.   _IO_default_xsgetn,
  278.   _IO_default_read,
  279.   _IO_default_write,
  280.   _IO_default_doallocate,
  281.   _IO_str_pbackfail,
  282.   _IO_default_setbuf,
  283.   _IO_default_sync,
  284.   _IO_str_finish,
  285.   _IO_default_close,
  286.   _IO_default_stat,
  287.   _IO_default_seek,
  288.   _IO_str_seekoff,
  289.   _IO_default_seekpos,
  290. };
  291.